<div id="register" class="login_wrapper">
    <div class="animate form login_form">
        <section class="login_content">
            <form action="javascript:void(0);">
                <h1>*** 管理系统注册</h1>
                <div>
                    <input type="text" v-model="user.name" class="form-control" placeholder="User Name" required="" />
                </div>
                <div>
                    <input type="text" v-model="user.email" class="form-control" placeholder="User Email" required="" />
                </div>
                <div>
                    <input type="password" v-model="user.password" class="form-control" placeholder="Password"
                        required="" />
                </div>
                <div>
                    <input type="password" v-model="newPassword" class="form-control" placeholder="Confirm Password"
                        required="" />
                </div>
                <div>
                    <a @click="register()" class="btn btn-default submit" href="javascript:void(0);">Register</a>
                    <a class="reset_pass" href="#">Lost your password?</a>
                </div>

                <div class="clearfix"></div>

                <div class="separator">
                    <p class="change_link">
                        <a href="/login" class="to_register"> Login in </a>
                    </p>

                    <div class="clearfix"></div>
                    <br />

                    <div>
                        <h1><i class="fa fa-paw"></i> Java Spring Boot!</h1>
                        <p>Java Spring Boot! ©2021 All Rights Reserved. </p>
                    </div>
                </div>
            </form>
        </section>
    </div>
</div>
<script>
    var register = new Vue({
        el: "#register",
        data: {
            user: {
                name: "",
                email: "",
                password: "",
                rememberMe: false,
            },
            newPassword: ""
        },
        methods: {
            register: function () {
                if (this.user.name === "" || this.user.email === "" || this.user.password === "" || this
                    .newPassword === "") {
                    this.$message({
                        message: "请输入完整注册信息！",
                        type: 'error'
                    });
                    return;
                } else if (this.user.password !== this.newPassword) {
                    this.$message({
                        message: "两次密码不一致！请重试",
                        type: 'error'
                    });
                } else {
                    console.log(this.user);
                    var self = this;
                    $.ajax({
                        url: "/api/user",
                        type: "post",
                        contentType: "application/json",
                        data: JSON.stringify(self.user),
                        success: function (rs) {
                            if (rs.status == 200) {
                                self.$message({
                                    message: "注册成功！即将前往登录",
                                    type: 'success'
                                });
                                window.location.href = "/login";
                            } else {
                                self.$message({
                                    message: rs.message,
                                    type: 'info'
                                });
                            }
                        },
                        error: function (data) {
                            self.$message({
                                message: "服务器走丢啦！",
                                type: 'error'
                            });
                        },
                    });
                }
            },
        },
    });
</script>